package dataImport.action.manager;

import java.io.Serializable;
import dataImport.action.CommandReader;
import dataImport.action.ConfigurationReader;
import dataImport.action.CustomActionReader;
import dataImport.action.DescriptionReader;
import dataImport.action.EmoteBeforeActionReader;
import dataImport.action.NpcReader;
import dataImport.action.ObjectReader;
import dataImport.action.PlaceReader;
import dataImport.action.PredefinedActionReader;
import dataImport.action.RequirementReader;
import dataImport.action.ScenarioReader;
import dataImport.action.StateReader;
import dataImport.action.abstracts.AbstractEntityReader;
import dataImport.action.helper.XMLReader;
import dataImport.action.manager.storage.EntityReaderStorage;
import dataImport.action.manager.storage.EntityReaderStorageKey;
import dataImport.exception.MissingReader;
import dataImport.exception.ReaderAlreadyFixedException;


public class EntityReaderManager implements Serializable {

	private static final long serialVersionUID = 7031878582298068304L;

	private EntityReaderStorage entityReaderStorage;

	public EntityReaderManager() {
		this.entityReaderStorage = new EntityReaderStorage();
	}

	public AbstractEntityReader getReader(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		return this.entityReaderStorage.get(entityReaderStorageKey);
	}

	public void addReaders(final EntityReaderStorageKey... entityReaderStorageKeys) throws MissingReader {
		this.entityReaderStorage.add(entityReaderStorageKeys);
	}

	public void addAllReaders() {
		try {
			this.entityReaderStorage.addAll();
		}
		catch (MissingReader e) {}
	}

	public void read() {
		for (AbstractEntityReader abstractEntityReader : this.entityReaderStorage.getAll()) {
			abstractEntityReader.read(XMLReader.getInstance().getDocument());
		}
	}

	public void fixEntities() throws ReaderAlreadyFixedException {
		try {
			this.getDescriptionReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getScenarioReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getCommandReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getCustomActionReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getPredefinedActionReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getEmoteBeforeActionReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getRequirementReader().fixEntities(this);
		}
		catch (MissingReader e) {}

		try {
			this.getConfigurationReader().fixEntities(this);
		}
		catch (MissingReader e) {}
	}

	public boolean contains(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		return this.entityReaderStorage.contains(entityReaderStorageKey);
	}

	public static final AbstractEntityReader createReader(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		switch (entityReaderStorageKey) {
			case COMMAND:
				return new CommandReader();
			case CUSTOM_ACTION:
				return new CustomActionReader();
			case DESCRIPTION:
				return new DescriptionReader();
			case NPC:
				return new NpcReader();
			case OBJECT:
				return new ObjectReader();
			case PLACE:
				return new PlaceReader();
			case PREDEFINED_ACTION:
				return new PredefinedActionReader();
			case REQUIREMENT:
				return new RequirementReader();
			case SCENARIO:
				return new ScenarioReader();
			case STATE:
				return new StateReader();
			case CONFIGURATION:
				return new ConfigurationReader();
			case EMOTEBEFOREACTION:
				return new EmoteBeforeActionReader();
		}

		throw new MissingReader(entityReaderStorageKey);
	}

	public CommandReader getCommandReader() throws MissingReader {
		return (CommandReader) this.entityReaderStorage.get(EntityReaderStorageKey.COMMAND);
	}

	public CustomActionReader getCustomActionReader() throws MissingReader {
		return (CustomActionReader) this.entityReaderStorage.get(EntityReaderStorageKey.CUSTOM_ACTION);
	}

	public DescriptionReader getDescriptionReader() throws MissingReader {
		return (DescriptionReader) this.entityReaderStorage.get(EntityReaderStorageKey.DESCRIPTION);
	}

	public NpcReader getNpcReader() throws MissingReader {
		return (NpcReader) this.entityReaderStorage.get(EntityReaderStorageKey.NPC);
	}

	public ObjectReader getObjectReader() throws MissingReader {
		return (ObjectReader) this.entityReaderStorage.get(EntityReaderStorageKey.OBJECT);
	}

	public PlaceReader getPlaceReader() throws MissingReader {
		return (PlaceReader) this.entityReaderStorage.get(EntityReaderStorageKey.PLACE);
	}

	public PredefinedActionReader getPredefinedActionReader() throws MissingReader {
		return (PredefinedActionReader) this.entityReaderStorage.get(EntityReaderStorageKey.PREDEFINED_ACTION);
	}

	public RequirementReader getRequirementReader() throws MissingReader {
		return (RequirementReader) this.entityReaderStorage.get(EntityReaderStorageKey.REQUIREMENT);
	}

	public ScenarioReader getScenarioReader() throws MissingReader {
		return (ScenarioReader) this.entityReaderStorage.get(EntityReaderStorageKey.SCENARIO);
	}

	public StateReader getStateReader() throws MissingReader {
		return (StateReader) this.entityReaderStorage.get(EntityReaderStorageKey.STATE);
	}

	public ConfigurationReader getConfigurationReader() throws MissingReader {
		return (ConfigurationReader) this.entityReaderStorage.get(EntityReaderStorageKey.CONFIGURATION);
	}

	public EmoteBeforeActionReader getEmoteBeforeActionReader() throws MissingReader {
		return (EmoteBeforeActionReader) this.entityReaderStorage.get(EntityReaderStorageKey.EMOTEBEFOREACTION);
	}
}
